/**
 * 〈所属模块〉
 * 〈功能描述〉
 *
 * @author Lcs
 * @date 2017/9/21 19:07
 */
import React from "react";
import PropTypes from "prop-types";

import FastImage from "react-native-fast-image";
import { Image, View, StyleSheet } from "react-native";
import { Color, FontSize } from "../../common/index";
import RegexUtil from "../../utils/RegexUtil";

const HolderImage = props => {
  const {
    style, source, defaultSource, fallbackSource, resizeMode, onLoadEnd,
  } = props;
  const hasChinese = source.uri && typeof source.uri === "string" && RegexUtil.checkChinese(source.uri);
  return (
    <View style={[style, { borderWidth: 0 }]}>
      {typeof source === "number" || hasChinese ? (
        <Image source={source} style={[styles.image, style]} defaultSource={defaultSource} />
      ) : (
        <FastImage
          source={source}
          style={[styles.image, style]}
          defaultSource={defaultSource}
          fallbackSource={fallbackSource || defaultSource}
          resizeMode={resizeMode}
          onLoadEnd={onLoadEnd}
        />
      )}
    </View>
  );
};

HolderImage.propTypes = {
  source: Image.propTypes.source.isRequired,
  defaultSource: Image.propTypes.source,
  fallbackSource: Image.propTypes.source,
  style: Image.propTypes.style,
  resizeMode: PropTypes.string,
  onLoadEnd: PropTypes.func,
};

HolderImage.defaultProps = {
  onLoadEnd: () => {},
  resizeMode: "contain",
  fallbackSource: require("../../img/ic_default_pic.png"),
  style: {},
  defaultSource: require("../../img/ic_default_pic.png"),
};
const styles = StyleSheet.create({
  text: {
    color: Color.white,
    fontSize: FontSize.SIZE_16,
  },
  image: {
    marginTop: 0,
    marginBottom: 0,
    marginLeft: 0,
    marginRight: 0,
    paddingTop: 0,
    paddingBottom: 0,
    paddingLeft: 0,
    paddingRight: 0,
    borderWidth: 0,
    backgroundColor: Color.transparent,
  },
});

export default HolderImage;
